import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/manager/xindong_chat_singleton.dart';
import 'package:xindong/models/chat_message/xindong_custom_message.dart';
import 'package:xindong/models/gift/xindong_gift_chat_class.dart';
import 'package:xindong/models/user/xindong_other_user.dart';
import 'package:xindong/routes/call/model/xindong_signal_class.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_dashan_tips.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_calling_balance_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_calling_cancel_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_calling_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_calling_end_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_gift_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_image_video_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_sound_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_text_cell.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_recharge_elem.dart';
import 'package:xindong/routes/tab_chats/xindong_chat_message_type.dart';
import 'package:xindong/utils/xindong_conversion_util.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

import 'xindong_liaotian_system_tips.dart';

class XinDongLiaoTianCustomMessageContainer extends StatelessWidget {
  final V2TimMessage message;
  final double maxWidth;
  final XinDongChatMessageType chatMessageType;
  final XinDongOtherUser? userClass;
  final Function() clearJump;

  const XinDongLiaoTianCustomMessageContainer(
      {Key? key,
      required this.message,
      required this.maxWidth,
      required this.chatMessageType,
      required this.clearJump,
      required this.userClass})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (message.elemType != MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) {
      // 版本不兼容
      return const XinDongLiaoTianSystemTips(text: kTextUpdateVersion);
    }
    if (chatMessageType.isSignalingMessage(message)) {
      return const SizedBox();
      // return XinDongLiaoTianMessageCustomSignaling(
      //   message: message,
      //   maxWidth: maxWidth,
      //   chatMessageType: chatMessageType,
      // );
    }
    try {
      mDebugPrint('mmmmmmm ${message.customElem?.data}');
      XinDongCustomMessage? model = XinDongConversionUtil.decodeJsonToMessageClass(message.customElem?.data ?? "");
      if (model != null) {
        // 文字聊是聊天消息
        final type = int.parse('${model.type}');
        switch (type) {
          case ChatType.daShan:
            return XinDongLiaoTianDaShanTips(model: model);
          case ChatType.text:
          case ChatType.sayHello:
            return XinDongLiaoTianMessageText(message: message, maxWidth: maxWidth);
          case ChatType.voice:
            message.soundElem = V2TimSoundElem()
              ..url = model.content
              ..duration = model.second;
            return XinDongLiaoTianMessageSound(message: message, maxWidth: maxWidth, clearJump: clearJump);
          case ChatType.image:
            return XinDongLiaoTianMessageImageVideo(message: message, url: model.content!);
          case ChatType.video:
            return XinDongLiaoTianMessageImageVideo(message: message, url: model.cover!, video: model.content);
          case ChatType.gift:
            if (model.data == null) break;
            final gift = XinDongGiftChatClass.fromJson(model.data!);
            return XinDongLiaoTianMessageGift(message: message, model: gift, score: model.score);
          case ChatType.callAudio:
          case ChatType.callVideo:
            if (model.content == null) break;
            final signal = XinDongSignalClass.fromJson(model.content);
            return XinDongLiaoTianMessageCalling(message: message, model: signal, type: type, maxWidth: maxWidth);
          case ChatType.callCancel:
            return XinDongLiaoTianMessageCallingCancel(message: message, model: model);
          case ChatType.callEnd:
            return XinDongLiaoTianMessageCallingEnd(message: message, model: model);
          case ChatType.poor:
            {
              Future<void> removeDuplicates(V2TimMessage currentMessage) async {
                String? userID = XinDongChatSingleton.instance.detailClass?.another.uid;
                final chatVM = XinDongChatSingleton.instance.currentChatSeparateVM;
                if (userID == null) return;
                // 原数据，这个数据进行删除操作会抛异常，需要创新临时数据去替换
                List<V2TimMessage> currentHistoryMsgList = chatVM.globalModel.messageListMap[userID] ?? [];
                // 临时数据，可以增删改查
                List<V2TimMessage> tempList = List<V2TimMessage>.from(currentHistoryMsgList);
                await TencentImSDKPlugin.v2TIMManager
                    .getMessageManager()
                    .deleteMessageFromLocalStorage(msgID: message.msgID!);
                tempList.remove(currentMessage);
                chatVM.globalModel.messageListMap[userID] = tempList;
                chatVM.notifyListeners();
              }
              Future.delayed(Duration.zero).whenComplete(() => removeDuplicates(message));
              return const SizedBox();
            }
          // return XinDongLiaoTianMessageCallingBalance(message: message);
          case XinDongCustomMessageType.characterRecharge:
            return XinDongLiaoTianRechargeElem(userClass: userClass);
          // case XinDongCustomMessageType.characterMessageWillConsume:
          //   return XinDongLiaoTianWillConsumeElem(
          //     userClass: userClass,
          //   );
          // case XinDongCustomMessageType.callingFinish:
          //   return XinDongLiaoTianSystemTips(text: model.data["msg"]);
          // case XinDongCustomMessageType.gameMessage:
          //   return XinDongLiaoTianGameElem(
          //     messageItem: message,
          //     model: XinDongEmotionMessageClass.fromJson(model.data),
          //   );
          // case XinDongCustomMessageType.gift:
          //   return XinDongLiaoTianGiftElem(
          //     message: message,
          //     model: XinDongGiftReceiveClass.fromJson(model.data),
          //   );
          // case XinDongCustomMessageType.characterRecharge:
          //   return XinDongLiaoTianRechargeElem(
          //     userClass: userClass,
          // //   );
          // case XinDongCustomMessageType.characterDiscount:
          //   return XinDongLiaoTianDiscountElem();
          // case XinDongCustomMessageType.characterReminder:
          //   return XinDongLiaoTianReminderView(text: model.data["content"]);
          // case XinDongCustomMessageType.characterPrice:
          //   return XinDongLiaoTianPriceView(text: model.data["content"]);
          default:
            return Text(
              model.content ?? "",
              textAlign: TextAlign.center,
              style: const TextStyle(fontSize: 12, color: DZColors.placeholderColor),
            );
        }
      }
    } catch (e) {
      mCatchPrint(e);
      /// 版本不兼容
      return const XinDongLiaoTianSystemTips(text: kTextUpdateVersion);
    }

    /// 版本不兼容
    return const XinDongLiaoTianSystemTips(text: kTextUpdateVersion);
  }
}
